<template>
  <!--登录-->
  <div class="wrap">
    <div class="tdbModule loginPage">
      <div class="registerTitle">用户登录</div>
      <div class="registerCont">
        <ul>

          <li>
            <span class="dis">手机号：</span>
            <input class="input" v-model="loginDto.mobile"/>
          </li>

          <li>
            <span class="dis">密码：</span>
            <input class="input" v-model="loginDto.password" type="password"/>
          </li>
          <li class="btn">
            <button @click="login()" :class="{ disabled: !isValid }">
              立即登录
            </button>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import '~/assets/css/register.css'
import cookie from 'js-cookie'

export default {
  data() {
    return {
      loginDto: {
        mobile: "",
        password: "",
        ip: "240e:468:d2:ad14:14da:f83b:c21f:8b41",
      },
      isValid: true, //表单校验是否成功
    }
  },


  methods: {

    //登录
    login: function () {
      //判断手机号是否为空
      if (this.loginDto.mobile == null) {
        this.$message.error('手机号不能为空,请填写');
        return false;
      }

      //判断手机号格式
      let myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(16[0-9]{1}))+\d{8})$/;
      if (!myreg.test(this.loginDto.mobile)) {
        this.$message.error('手机号格式不正确,请重新填写');
        return false;
      }

      //登录
      this.$axios.$post("/userInfo/login", this.loginDto)
          .then(response => {
            if (!response.data) {
              this.$message.error(response.message);
              return false;
            } else {
              this.$message.success(response.message)
              // 把用户信息存在cookie中
              cookie.set('userInfo', response.data.data)
              window.location.href='/user'
            }
          })
    },
  },
}
</script>
